﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminChipTagsGroups : Form
    {
        private aDbDataSet.ChipTagsGroupsRow drActiveGroup;
        private bool bNew;
        
        public frmAdminChipTagsGroups()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Load
        private void frmAdminChipTagsGroups_Load(object sender, EventArgs e)
        {
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;
            this.taChipTagsGroups.Fill(this.aDbDataSet.ChipTagsGroups);
        }

        // Selection changed
        private void chipTagsGroupsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvChipTagsGroups.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)dgvChipTagsGroups.CurrentRow.DataBoundItem;
                chkActive.Checked = Convert.ToBoolean(drv["Active"]);
                txtName.Text = drv["Name"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                chkActive.Checked = false;
                txtName.Text = string.Empty;

                this.drActiveGroup = aDbDataSet.ChipTagsGroups.NewChipTagsGroupsRow();

                this.drActiveGroup["Active"] = true;
                this.drActiveGroup["Sync"] = false;
                this.drActiveGroup["WebId"] = 0;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                DataRowView drv = (DataRowView)dgvChipTagsGroups.CurrentRow.DataBoundItem;
                this.drActiveGroup = (aDbDataSet.ChipTagsGroupsRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            chipTagsGroupsDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte názov!", "Chip Tags - skupiny - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                this.drActiveGroup["Active"] = chkActive.Checked;
                this.drActiveGroup["Name"] = txtName.Text;
                this.drActiveGroup["Modified"] = DateTime.Now;

                if (bNew)
                {
                    aDbDataSet.ChipTagsGroups.Rows.Add(this.drActiveGroup);
                    this.taChipTagsGroups.Update(aDbDataSet.ChipTagsGroups);
                }
                else
                {
                    this.taChipTagsGroups.Update(this.drActiveGroup);
                }

                this.taChipTagsGroups.Fill(this.aDbDataSet.ChipTagsGroups);

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;

                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Chip Tags - skupiny - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbChipTags_Click(object sender, EventArgs e)
        {
            if (this.dgvChipTagsGroups.CurrentRow == null)
                return;
            
            DataRowView drv = (DataRowView) this.dgvChipTagsGroups.CurrentRow.DataBoundItem;
            frmAdminChipTags frm = new frmAdminChipTags(Convert.ToInt32(drv["ID"]), drv["Name"].ToString());
            frm.ShowDialog();
        }

        private void dgvChipTagsGroups_DoubleClick(object sender, EventArgs e)
        {
            tsbChipTags_Click(null, null);
        }

        private void tsbActive_Click(object sender, EventArgs e)
        {
            if (this.dgvChipTagsGroups.CurrentRow == null)
                return;

            int groupID = Convert.ToInt32(((DataRowView)this.dgvChipTagsGroups.CurrentRow.DataBoundItem)["ID"]);

            DataTable dtAll = this.taChipTagsGroups.GetData();
            foreach (DataRow drComp in dtAll.Rows)
            {
                if (Convert.ToInt32(drComp["ID"]) == groupID)
                    drComp["Active"] = true;
                else
                    drComp["Active"] = false;
            }

            // Save + Reload
            this.taChipTagsGroups.Update((aDbDataSet.ChipTagsGroupsDataTable) dtAll);
            this.frmAdminChipTagsGroups_Load(null, null);
        }

        
        

        

        

        

        

        
    }
}
